<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="800">
    <el-row>
      <el-col :xl="24"> 标题:{{ formData.title }} </el-col></el-row
    >
    <el-row
      ><el-col :xl="24"> 内容: <div v-html="formData.content"></div> </el-col
    ></el-row>
    <el-row
      ><el-col :xl="24">时间:{{ formatTime(formData.createTime, 'yyyy-MM-dd') }} </el-col></el-row
    >
    <el-row
      ><el-col :xl="24">备注:{{ formData.remark }}</el-col></el-row
    >
    <template #footer>
      <el-button @click="dialogVisible = false">确 定</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
import { CommonStatusEnum } from '@/utils/constants'
import * as NoticeApi from '@/api/system/notice'
import { formatTime } from '@/utils'

defineOptions({ name: 'SystemNoticeForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  title: '',
  type: undefined,
  content: '',
  status: CommonStatusEnum.ENABLE,
  remark: ''
})

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await NoticeApi.getNotice(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>
